package com.ciwong.mobilelib.utils.animtoast;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;

public class ToastAnimContal {
    protected static void addAnim(final Context context, final View toastView, final View textView, final WindowManager mWindowManager,
                                  final WindowManager.LayoutParams windowParams, final long duration, final IFinishAnimToast animFinish) {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setDuration(duration / 4);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float mpro = (float) animation.getAnimatedValue();
                windowParams.y = (int) (context.getResources().getDisplayMetrics().widthPixels / 5 * mpro);
                mWindowManager.updateViewLayout(toastView, windowParams);
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                ObjectAnimator translationY = new ObjectAnimator().ofFloat(textView, "translationY", textView.getHeight(), 0f);
                translationY.setStartDelay(duration / 20);
                translationY.setInterpolator(new DecelerateInterpolator());
                translationY.setDuration(duration * 3 / 20);
                translationY.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        textView.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        if (toastView == null) return;
                        final int height = toastView.getHeight();
                        final int width = toastView.getWidth();
                        ObjectAnimator animXY = ObjectAnimator
                                .ofFloat(toastView, "scaleY", 1f, 1.1f)
                                .ofFloat(toastView, "scaleX", 1f, 1.1f);
                        animXY.setDuration(duration / 2)
                                .setStartDelay(duration / 20);
                        animXY.setInterpolator(new AccelerateDecelerateInterpolator());
                        animXY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                float value = (Float) animation.getAnimatedValue();
                                if (toastView == null) return;
                                Log.d("lzh toastViewHeight: ", value + ":" + toastView.getWidth() + ":" + toastView.getHeight());
                                windowParams.height = (int) (height * (value));
                                windowParams.width = (int) (width * (value));
                                mWindowManager.updateViewLayout(toastView, windowParams);
                            }
                        });
                        animXY.start();
                        toastView.animate()
                                .alpha(0.0f)
                                .setDuration(duration / 2)
                                .setStartDelay(duration / 20)
                                .setInterpolator(new AccelerateDecelerateInterpolator())
                                .setListener(new Animator.AnimatorListener() {
                                    @Override
                                    public void onAnimationStart(Animator animation) {

                                    }

                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        if (animFinish != null)
                                            animFinish.animFiish(toastView);
                                    }

                                    @Override
                                    public void onAnimationCancel(Animator animation) {

                                    }

                                    @Override
                                    public void onAnimationRepeat(Animator animation) {

                                    }
                                })
                                .start();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }
                });
                translationY.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        valueAnimator.start();
    }


    protected static void addAnim(Context context, final View toastView, final TextView mTextView, final long calculationDuration, final IFinishAnimToast animFinish) {
        toastView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                toastView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                ObjectAnimator translationY = new ObjectAnimator().ofFloat(mTextView, "translationY", mTextView.getHeight(), 0f);
                translationY.setStartDelay(calculationDuration / 10);
                translationY.setInterpolator(new DecelerateInterpolator());
                translationY.setDuration(calculationDuration * 7 / 20);
                translationY.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        mTextView.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        toastView.animate()
                                .alpha(0.0f)
                                .scaleX(1.1f)
                                .scaleY(1.1f)
                                .setDuration(calculationDuration * 2 / 5)
                                .setStartDelay(calculationDuration * 3 / 20)
                                .setListener(new Animator.AnimatorListener() {
                                    @Override
                                    public void onAnimationStart(Animator animation) {

                                    }

                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        if (animFinish != null)
                                            animFinish.animFiish(toastView);
                                    }

                                    @Override
                                    public void onAnimationCancel(Animator animation) {

                                    }

                                    @Override
                                    public void onAnimationRepeat(Animator animation) {

                                    }
                                })
                                .start();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                translationY.start();
            }
        });
    }


}
